//
//  LSStatusPictureView.swift
//  MicroBlog
//
//  Created by lj on 15/9/9.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

let LSStatusPictureViewSelectedphotoNotification = "LSStatusPictureViewSelectedphotoNotification"
let LSStatusPictureViewSelectedphotoURLsKey = "LSStatusPictureViewSelectedphotoURLsKey"
let LSStatusPictureViewSelectedphotoIndexPath = "LSStatusPictureViewSelectedphotoIndexPath"

let LSStatusPictureViewCellID = "LSStatusPictureViewCellID"

class LSStatusPictureView: UICollectionView {

    ///微博视图模型
    var statusViewModel: LSStatusViewModel? {
    
        didSet {
            sizeToFit()
            
            reloadData()
        }
    }
    //siziToFit会调用这个方法
    override func sizeThatFits(size: CGSize) -> CGSize {
        return calcViewSize()
    }
    
    /// 根据图片数量计算视图大小

    private func calcViewSize() -> CGSize {
        
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        //设置默认的大小
        layout.itemSize = CGSize(width: LSStatusPictureItemWidth, height: LSStatusPictureItemWidth)
        
        let count = statusViewModel?.thumbnailURLs?.count ?? 0
        
        switch(count) {
        case 0 :
            return CGSizeZero
        case 1 :
            var size = CGSize(width: 150, height: 150)
            
            let key = statusViewModel?.thumbnailURLs![0].absoluteString
            
            if let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(key) {
                size = image.size
            }
            
            size.width = size.width < 40 ? 150 : size.width
            size.width = size.width > 300 ? 150 : size.width
            
            layout.itemSize = size
            return size
        case 4 :
            let width = 2 * LSStatusPictureItemWidth + LSStatusPictureItemMargin
            return CGSize(width: width, height: width)
        default:
            let row = CGFloat((count - 1) / Int(LSStatusPictureMaxCount ) + 1)
            let height = row * LSStatusPictureItemWidth + (row - 1) * LSStatusPictureItemMargin
            let width = LSStatusPictureMaxWidth

//            printLog(count)
//            printLog(row)
//            printLog(height)
            
            return CGSize(width: width, height: height)
        }

    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        backgroundColor = UIColor(white: 0.98, alpha: 1.0)
        
        //设置布局间距
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.minimumInteritemSpacing = LSStatusPictureItemMargin
        layout.minimumLineSpacing = LSStatusPictureItemMargin
        
        //指定数据源与代理
        dataSource = self
        delegate = self
        
        registerClass(LSStatusPictureViewCell.self, forCellWithReuseIdentifier: LSStatusPictureViewCellID)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

//MARK:UICollectionViewDataSource,UICollectionViewDelegate
extension LSStatusPictureView: UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return statusViewModel?.thumbnailURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(LSStatusPictureViewCellID, forIndexPath: indexPath) as! LSStatusPictureViewCell
        
        cell.imageURL = statusViewModel!.thumbnailURLs![indexPath.row]
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        //发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(LSStatusPictureViewSelectedphotoNotification,
            object: self, userInfo:
            [LSStatusPictureViewSelectedphotoIndexPath:indexPath,
            LSStatusPictureViewSelectedphotoURLsKey:statusViewModel!.bmiddleURLs!])
    }
   
}



private class LSStatusPictureViewCell:UICollectionViewCell {
    
    var imageURL:NSURL? {
        didSet {
            thumbnailView.sd_setImageWithURL(imageURL)
        }
    }
    
    //MARK :构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(thumbnailView)
        thumbnailView.ff_Fill(self)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //
    private lazy var thumbnailView:UIImageView = UIImageView()
}
